// pages/coles-search/search.js
import {
  ajax,
  src,
  util
} from "../../utils/util.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopname: '',
    shoplist: ''
  },
  //展示更多优惠
  zhanshi: function (e) {
    var idx = e.currentTarget.dataset.upid;
    if (this.data.shoplist[idx].shownums == 1) {
      var str = "shoplist[" + idx + "].shownums"
      this.setData({
        [str]: 100
      })
    } else {
      var str = "shoplist[" + idx + "].shownums"
      this.setData({
        [str]: 1
      })
    }
    console.log(this.data.shoplist, 'list')
  },
  // 点击查看更多
  seeMore: function (e) {
    var idx = e.currentTarget.dataset.seeid;
    console.log(idx, 'idx')

    if (this.data.shoplist[idx].shownum == 2) {
      var str = "shoplist[" + idx + "].shownum"
      this.setData({
        [str]: 100
      })
    } else {
      var str = "shoplist[" + idx + "].shownum"
      this.setData({
        [str]: 2
      })
    }
  },
  //获取输入的商品名称  
  shopName: function (e) {
    this.setData({
      shopname: e.detail.value
    })
  },
  //跳转店铺主页
  homepage: function (e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/shop-homepage/homepage?ID=' + id,
    })
  },
  //跳转商品详情
  shopdetails: function (e) {
    let goodsid = e.currentTarget.dataset.goodsid;
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/supermarket-details/details?shopID=' + id + '&ID=' + goodsid,
    })
  },
  //输入name后获取列表
  getlist: function () {
    if (this.data.shopname) {
      ajax('/api/Homesearch/Get?requestContent=' + JSON.stringify({
        token: wx.getStorageSync('Access_token'),
        ShopName: this.data.shopname,
        Province: wx.getStorageSync('address_component').province,
        City: wx.getStorageSync('address_component').city,
        District: wx.getStorageSync('address_component').district,
        lng: wx.getStorageSync('longitude'),
        lat: wx.getStorageSync('latitude')
      }), {
          header: {
            'Accept': 'application/x-www-form-urlencoded', // 默认值
          },
          method: "get",
        }, res => {
          // console.log(res)
          var shoplist = res.Models
          for (var x in shoplist) {
            shoplist[x].shownum = 2
            shoplist[x].shownums = 1
          }
          this.setData({
            shoplist: shoplist
          })
        })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#000',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})